package com.kaka.bus.engine.home.info.changepwd;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/11/15.
 */

public class ChangePresenter extends RxPresenter<ChangeContract.View> implements ChangeContract.Presenter {

    @Inject
    public ChangePresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void changePwd(String userId, String token, String oldPwd, String newPwd, String newPwd2, String tokenkey) {
        if (!(oldPwd != null && newPwd != null && newPwd2 != null)) {
            mView.paramsFailed("原密码和新密码不能为空!");
            return;
        }

        if (!newPwd.equals(newPwd2)) {
            mView.paramsFailed("2次输入的新密码必须保持一致!");
            return;
        }

        if (oldPwd.length() < 6) {
            mView.paramsFailed("原密码必须为6位数英文或数字!");
            return;
        }

        if (newPwd.length() < 6) {
            mView.paramsFailed("新密码必须为6位数英文或数字!");
            return;
        }
        mView.showDialog("修改密码..");
        addSubscribe(mDataManager
                .getApi()
                .personUpdatePassword(userId, oldPwd, newPwd, tokenkey, token, Constant.PLATFORM)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> stringHttpWrapper) {
                        mView.onSuccess(stringHttpWrapper);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
    }
}
